package com.neuedu.dao.impl;

import com.neuedu.common.DBUtil;
import com.neuedu.dao.BaseDao;
import com.neuedu.dao.WorkRuleDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class WorkRuleDaoImpl extends BaseDao implements WorkRuleDao {

    @Override
    public int addWorkRule(Object[] objs, Connection con) throws SQLException, ParseException {
        String sql = "INSERT INTO workrule(rulename,deptid,startdate,enddate) VALUES(?,?,?,?)";
        String sql2 = "select max(id) from workrule";
        new Date();
        new Date();
        int result = 0;
        int id = 0;
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, objs[0].toString());
        ps.setInt(2, Integer.parseInt(objs[1].toString()));
        String startdate = objs[2].toString();
        String enddate = objs[3].toString();
        Date dateStart = (new SimpleDateFormat("yyyy-MM-dd")).parse(startdate);
        java.sql.Date sqlDate1 = new java.sql.Date(dateStart.getTime());
        Date dateEnd = (new SimpleDateFormat("yyyy-MM-dd")).parse(enddate);
        java.sql.Date sqlDate2 = new java.sql.Date(dateEnd.getTime());
        ps.setDate(3, sqlDate1);
        ps.setDate(4, sqlDate2);
        result = ps.executeUpdate();
        PreparedStatement ps2 = con.prepareStatement(sql2);
        ResultSet rs = ps2.executeQuery();
        if (rs.next()) {
            id = rs.getInt(1);
        }
        return id;
    }

    @Override
    public List<Map<String, Object>> queryWorkRule(String sql) {
        Connection con = DBUtil.getCon();
        return this.querySql(con, sql);
    }
}
